(function($){
	$.fn.customerInit = function(){
		var $this = $(this);
		var selectRow = null;
		var selectIndex = null;
		
		var editDialog= $('#editDialog',$this);
		var cardDialog= $('#cardDialog',$this);
		//客户类型
		$('#item',$this).combobox({
			data:TEA.getCustomerTypeList(),
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:150
		})
		//编辑界面客户类型
		$('#customerTypeID',editDialog).combobox({
			data:TEA.getCustomerTypeList(),
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:250
		})
		//客户来源
		$('#customerSourceID',editDialog).combobox({
			data:TEA.getCustomerSourceList(),
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:250
		})
		//查询
		$('#search',$this).click(function(){
			var queryContent = $('.queryContent',$this);
			var customerTypeID = $('#item',$this).combobox('getValue');
			var customerCode = $('#customerCode',queryContent).val();
			var customerName = $('#customerName',queryContent).val();
			
			var content = {customerTypeID:customerTypeID,customerCode:customerCode,customerName:customerName};
			
			$('#customerList',$this).datagrid({
				queryParams:content,
				pageNumber:1
			});
		})
		//添加
		var onAdd = function(){
			$(editDialog).dialog('open');
		} 
		//修改
		var onUpdate = function(){
			if(selectRow==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			$(editDialog).dialog('open');
			$('#customerForm',editDialog).form('load',selectRow);
		}
		//编辑积分卡
		var onCard = function(){
			if(selectRow==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			var url ='card!get.action';
			var content = {'customer.customerID':selectRow.customerID};
			asyncCallService(url,content,function(result){
				if(result.success){
					$('#cardForm',cardDialog).form('load',result);
				}else{
					$.messager.alert("提示","获取数据失败","warning");
				}
			})
			$(cardDialog).dialog('open');
		}
		//刷新列表
		var onReload = function(){
			$('#customerList',$this).datagrid('reload');
		}
		//加载列表
		$('#customerList',$this).datagrid({
			url:"customer!select.action",
			singleSelect:true,
			fit:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:true,
			pageSize:30,
			toolbar:[	
						{id:'add',text:'添加',iconCls:'icon-add',handler:function(){onAdd()}},
						{id:'update',text:'修改',iconCls:'icon-edit',handler:function(){onUpdate()}},
						{id:'cardSet',text:'积分卡',iconCls:'icon-view',handler:function(){onCard()}},
						{id:'reload',text:'刷新',iconCls:'icon-reload',handler:function(){onReload()}}
					],
			columns:[[
				{field:'customerID',hidden:true},
				{field:'customerTypeID',hidden:true},
				{field:'customerSourceID',hidden:true},
				{field:'customerCode',title:'客户编号',width:100,align:"center"},
				{field:'customerName',title:'客户名称',width:100,align:"center"},
				{field:'customerTypeName',title:'客户类型',width:100,align:"center"},
				{field:'customerSourceName',title:'客户来源',width:100,align:"center"},
				{field:'sex',title:'性别',width:100,align:"center"},
				{field:'birthday',title:'生日',width:100,align:"center"},
				{field:'character',title:'性格特点',width:100,align:"center"},
				{field:'company',title:'公司',width:200,align:"center"},
				{field:'roleName',title:'职位',width:100,align:"center"},
				{field:'telephone',title:'电话',width:100,align:"center"},
				{field:'address',title:'地址',width:100,align:"center"},
				{field:'note',title:'备注',width:300,align:"center"}
			]],
			onClickRow:function(rowIndex, rowData){
				selectRow = rowData;
				selectIndex = rowIndex;
			},
			onDblClickRow:function(rowIndex,rowData){
				onUpdate();
			},
			onLoadSuccess:function(){
				selectRow = null;
		 		selectIndex = null;
			}
		});
		
		var onSave = function(){
			var customerCode = $('#customerCode',editDialog).val();
			if($.trim(customerCode)==''){
				$.messager.alert('提示','客户编号不能为空','warning');
				return;
			}
			var customerName = $('#customerName',editDialog).val();
			if($.trim(customerName)==''){
				$.messager.alert('提示','客户名称不能为空','warning');
				return;
			}
			var url = null;
			if($('#customerID',editDialog).val()!=''){
				url= 'customer!update.action';
			}else{
				url= 'customer!add.action';
			}
			$('#customerForm',editDialog).ajaxSubmit({
				url:url,
				type:'post',
				dataType:'json',
				success:function(result){	
					if(result.success){
						var fn = function(){
							//如果是新增,需要重新查询页数
							if($('#customerID',editDialog).val()!=''){
								var row = $('#customerForm',editDialog).serializeObject();
								$('#customerList',$this).datagrid('updateRow',{index:selectIndex,row:row});	
							}else{
								onReload();
							}
							onExit();
						}
						$.messager.alert('提示','保存成功','info',fn);
					}else{
						$.messager.alert('提示',result.msg,"warning");
					}
				}
			});
		}
		
		var onExit = function(){
			$(editDialog).dialog('close');
			$('#customerForm',editDialog).form('clear');
		}
		
		$(editDialog).dialog({  
		    title: '编辑客户',  
		    width:750,
		    height:500,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'保存',iconCls:'icon-save',handler:function(){onSave()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onExit()}
			}]
		});
		//积分卡保存
		var onCardSave = function(){
			var cardCode = $('#cardCode',cardDialog).val();
			if($.trim(cardCode)==''){
				$.messager.alert('提示','卡号不能为空','warning');
				return;
			}
			var url = null;
			if($('#cardID',cardDialog).val()!=''){
				url= 'card!update.action';
			}else{
				url= 'card!add.action';
			}
			var content = {'customer.customerID':selectRow.customerID};
			$('#cardForm',cardDialog).ajaxSubmit({
				url:url,
				data:content,
				type:'post',
				dataType:'json',
				success:function(result){	
					if(result.success){
						$.messager.alert('提示','保存成功','info',onCardExit);
					}else{
						$.messager.alert('提示',result.msg,"warning");
					}
				}
			});
		}
		//积分卡退出
		var onCardExit = function(){
			$('#cardForm',cardDialog).form('clear');
			$(cardDialog).dialog('close');
		}
		
		$(cardDialog).dialog({  
		    title: '编辑积分卡',  
		    width:450,
		    height:400,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'保存',iconCls:'icon-save',handler:function(){onCardSave()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onCardExit()}
			}]
		});
	}
})(jQuery);